package com.meiyu.live.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.umeng.socialize.UMShareAPI;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.util.DisplayUtils;
import com.meiyu.live.widget.PagerSlidingTabStrip;

public class ChooseLiveActivity extends BaseActivity implements View.OnClickListener {

    private String[] tabName = {"直播", "约播"};

    private PagerSlidingTabStrip tabStrip;
    private ViewPager viewPager;
    private HomePagerAdapter pagerAdapter;
    private int hook_status;
    private View btn_back;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chooselive);
        hook_status = getIntent().getIntExtra(MainActivity.HOOK_STATUS, 1);
        init();
    }

    private void init() {
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        tabStrip = (PagerSlidingTabStrip) findViewById(R.id.tab_strip);
        tabStrip.setTextSize(DisplayUtils.dpToPixel(this, 22));
        tabStrip.setTextColorResource(R.color.white);
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        pagerAdapter = new ChooseLiveActivity.HomePagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabStrip.setViewPager(viewPager);
        viewPager.setCurrentItem(hook_status);
        pagerAdapter = new HomePagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabStrip.setViewPager(viewPager);
        viewPager.setCurrentItem(hook_status);

    }


    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        }
    }

    class HomePagerAdapter extends FragmentPagerAdapter {

        public HomePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return tabName.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabName[position];
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void startUpdate(ViewGroup container) {
            super.startUpdate(container);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0: {
                    fragment = StartLiveFragment.newInstance(hook_status);
                }
                break;
                case 1: {

                    if (hook_status == 0) {
                        fragment = Apply1Fragment.newInstance();
                    } else {
                        fragment = StartHookFragment.newInstance();
                    }
                }
                break;
            }
            return fragment;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

}
